require(["common", "jQuery", "coAjax", "queryString", "hash_routie"], function(jSouper, $, coAjax, QueryString, hash_routie) {
	var queryString;
	var _card_factory_id = null;
	var _classify_id = null;
	var _num = null;
	var _page = null;
	var classify_api_root = appConfig.bus;

	var CLASSIFY_TYPE_array = [];
	jSouper.forEach(appConfig.CLASSIFY_TYPE, function(value, key) {
		if (key == "无") {
			key = "无（跟随父级目录优惠配置）"
		}
		CLASSIFY_TYPE_array.push({
			key: key,
			value: value
		});
	});
	App.set("$Cache.CLASSIFY_TYPE_array", CLASSIFY_TYPE_array);


	hash_routie.on("classify", function() {
		queryString = new QueryString(location.hash);
		//获取card_factory列表
		coAjax.get(appConfig.bus.card_factory_list, function(result) {
			App.set("$Cache.classify.card_factory_list", result.result)
		});
		//获取card_factory信息，改变所有接口
		var card_factory_id = queryString.get("card_factory_id");
		if (card_factory_id !== _card_factory_id) {
			_card_factory_id = card_factory_id;
			_classify_id = _num = _page = null;
			//锁定使用公共商品目录
			// if (_card_factory_id) {
			// 	classify_api_root = JSON.parse(JSON.stringify(appConfig.bus.c_f_c).replace(/\:card_factory_id/g, _card_factory_id));
			// } else {
			// 	classify_api_root = appConfig.bus;
			// }
		}
		//获取分类
		//classify_id不存在的话默认会返回根目录内容
		var classify_id = queryString.get("classify_id");
		if (classify_id !== _classify_id) {
			_classify_id = classify_id;
			coAjax.get(classify_api_root.classify_by_id + classify_id, function(result) {
				App.set("$Cache.classify.info", result.result);
			});
			App.set("$Cache.classify.pointer_classify_vms", []);
			App.set("$Cache.classify.current_classify", null);
		}
		//获取商品
		var num = queryString.get("num") || 10;
		var page = queryString.get("page") || 0;
		if (num !== _num || page !== _page) {
			_num = num;
			_page = page;
			if (_card_factory_id) {
				var goods_list_url = appConfig.card_factory.goods_list_with_page_info + _card_factory_id;
			} else {
				goods_list_url = appConfig.goods.list_with_page_info;
			}
			coAjax.get(goods_list_url, {
				bus_id: busInfo._id,
				num: num,
				page: page
			}, function(result) {
				App.set("$Cache.bus.goods_with_page_info", result.result);
			});
		}
	});
	//导航栏的跳转
	App.set("$Event.classify.nav.to_root", function() {
		hash_routie.hash({
			classify_id: ""
		});
	});
	App.set("$Event.classify.nav.to_dir", function(e, vm) {
		hash_routie.hash({
			classify_id: vm.get("_id")
		});
	});
	App.set("$Event.classify.nav.back", function() {
		hash_routie.hash({
			classify_id: App.get("$Cache.classify.info.current_classify._id")
		});
	});
	//商品目录的跳转
	App.set("$Event.classify_to_card_factory", function(e, vm) {
		hash_routie.hash({
			card_factory_id: this.getAttribute("card-factory-id") || "",
			page: 0
		});
	});
	//显示右键功能菜单
	App.set("$Event.classify.show_menu_box", function(e, vm) {
		//显示菜单
		App.set("$Cache.classify.menu_box.show", true);

		var offset = this.getBoundingClientRect();
		var _re_y = e.pageY - offset.top - scrollY;
		var _re_x = e.pageX - offset.left - scrollX;
		vm.set("$Cache.classify.menu_box.top", _re_y);
		vm.set("$Cache.classify.menu_box.left", _re_x);

		App.set("$Cache.classify.menu_box.pointer_classify_vms", App.get("$Cache.classify.pointer_classify_vms"));
		App.set("$Cache.classify.menu_box.pointer_goods_vms", App.get("$Cache.classify.pointer_goods_vms"));
		return false;
	});
	App.set("$Event.classify.force_hidden_menu_box", function() {
		App.set("$Cache.classify.menu_box.show", false);
		App.set("$Cache.classify.menu_box.pointer_classify_vms", []);
		App.set("$Cache.classify.menu_box.pointer_goods_vms", []);
	});
	App.set("$Event.classify.hidden_menu_box", function() {
		if (!App.get("$Cache.classify.menu_box.lock")) {
			console.log("hidden")
			App.get("$Event.classify.force_hidden_menu_box")();
		}
	});
	//锁定，不可隐藏
	App.set("$Event.classify.menu_box.lock_blur", function() {
		console.log("lock")
		App.set("$Cache.classify.menu_box.lock", true);
	});
	App.set("$Event.classify.menu_box.unlock_blur", function() {
		console.log("unlock")
		App.set("$Cache.classify.menu_box.lock", false);
	});
	//保存文件夹对象
	App.set("$Event.classify.store_classify_vm", function(e, vm) {
		App.push("$Cache.classify.pointer_classify_vms", vm);
	});
	//移除文件夹对象
	App.set("$Event.classify.release_classify_vm", function(e, vm) {
		App.popItem("$Cache.classify.pointer_classify_vms", vm);
	});
	//打开文件夹
	App.set("$Event.classify.open_classify", function(e, vm) {
		hash_routie.hash({
			classify_id: vm.get("_id")
		});
	});
	//保存商品对象
	App.set("$Event.classify.store_goods_vm", function(e, vm) {
		App.push("$Cache.classify.pointer_goods_vms", vm);
	});
	//移除商品对象
	App.set("$Event.classify.release_goods_vm", function(e, vm) {
		App.popItem("$Cache.classify.pointer_goods_vms", vm);
	});
	//添加文件夹
	App.set("$Event.classify.new_classify", function(e, vm) {
		myPrompt("请输入文件夹名：", function(new_classify_name) {
			coAjax.post(classify_api_root.classify_add, {
				parent_classify_id: queryString.get("classify_id"),
				classify_name: new_classify_name
			}, function(result) {
				App.push("$Cache.classify.info.child_classify_list", result.result);
			});
		});
		App.get("$Event.classify.force_hidden_menu_box")();
	});
	//删除
	App.set("$Event.classify.remove_classify", function(e, vm) {
		var classify_id_list = jSouper.map(App.get("$Cache.classify.menu_box.pointer_classify_vms"), function(classify_vms) {
			return classify_vms.get("_id");
		});
		myConfirm("确定要删除这" + classify_id_list.length + "个文件夹（分类）？", function() {
			coAjax["delete"](classify_api_root.classify_remove_list, {
				classify_id_list: classify_id_list,
				return_classify_id: queryString.get("classify_id")
			}, function(result) {
				alert("success", "删除成功");
				App.set("$Cache.classify.info", result.result);
			});
		});
		App.get("$Event.classify.force_hidden_menu_box")();
	});
	//修改属性
	App.set("$Event.classify.show_update_classify", function() {
		App.set("$Cache.classify.menu_box.show_update_classify", true);
		current_editting_classify_vm = App.get("$Cache.classify.menu_box.pointer_classify_vms")[0] || current_classify_m;
		App.set("$Cache.classify.current_editting_classify.info", current_editting_classify_vm.get());
		App.get("$Event.classify.force_hidden_menu_box")();
	});
	App.set("$Event.classify.confirm_update_classify", function() {
		var classify_info = App.get("$Cache.classify.current_editting_classify.info");
		coAjax.put(classify_api_root.classify_update + classify_info._id, classify_info, function(result) {
			current_editting_classify_vm.set(result.result);
			alert("修改完成");
			App.set("$Cache.classify.menu_box.show_update_classify", false);
		});
	});
	App.set("$Event.classify.cancel_update_classify", function() {
		App.set("$Cache.classify.menu_box.show_update_classify", false);
	});


	var current_editting_classify_vm;
	var current_classify_m = App.getModel().buildModelByKey("$Cache.classify.info.current_classify");
	//初始化属性配置面板
	function _init_config_panle() {
		//数据绑定编辑
		current_editting_classify_vm = App.get("$Cache.classify.menu_box.pointer_classify_vms")[0] || current_classify_m;
		var card_factory_config_list = current_editting_classify_vm.get("card_factory_config_list");
		if (card_factory_config_list instanceof Array) {
			//数据整理，为了让数组之间一一对应
			var new_arr = [];
			jSouper.forEach(App.get("$Cache.classify.card_factory_list"), function(card_factory) {
				var item;
				jSouper.forEach(card_factory_config_list, function(card_factory_config) {
					if (card_factory_config.card_factory_id == card_factory._id) {
						item = card_factory_config;
						return false;
					}
				});
				new_arr.push(item || {
					card_factory_id: card_factory._id
				});
			});
			card_factory_config_list = new_arr;
		} else {
			card_factory_config_list = jSouper.map(App.get("$Cache.classify.card_factory_list"), function(card_factory) {
				return {
					card_factory_id: card_factory._id
				}
			});
		}
		current_editting_classify_vm.set("card_factory_config_list", card_factory_config_list);
		App.set("$Cache.classify.current_editting_classify.info", current_editting_classify_vm.get());
		//关闭右键菜单
		App.get("$Event.classify.force_hidden_menu_box")();
	};
	//打开启用优惠配置面板
	App.set("$Event.classify.show_enable_buff_classify", function() {
		//显示面板
		App.set("$Cache.classify.menu_box.show_enable_buff_classify", true);
		_init_config_panle();
	});
	//修改优惠信息面板
	App.set("$Event.classify.show_update_buff_classify", function() {
		//显示面板
		App.set("$Cache.classify.menu_box.show_update_buff_classify", true);
		_init_config_panle();
	});
	//停用优惠面板
	App.set("$Event.classify.show_disable_buff_classify", function() {
		//显示面板
		App.set("$Cache.classify.menu_box.show_disable_buff_classify", true);
		_init_config_panle();
	});
	//关闭所有配置面板
	App.set("$Event.classify.cancel_config_classify", function() {
		App.set("$Cache.classify.menu_box.show_enable_buff_classify", false);
		App.set("$Cache.classify.menu_box.show_update_buff_classify", false);
		App.set("$Cache.classify.menu_box.show_disable_buff_classify", false);
		App.get("$Event.classify.force_hidden_menu_box")();
	});
	//启用优惠配置
	App.set("$Event.classify.enable_buff_classify", function() {
		coAjax.put(classify_api_root.classify_enable_buff + current_editting_classify_vm.get("_id"),
			App.get("$Cache.classify.current_editting_classify"),
			function(result) {
				alert("success", "优惠配置启动成功");
				current_editting_classify_vm.set(result.result);
				App.get("$Event.classify.cancel_config_classify")();
			});
	});
	//修改优惠配置
	App.set("$Event.classify.update_buff_classify", function() {
		coAjax.put(classify_api_root.classify_update + current_editting_classify_vm.get("_id"),
			App.get("$Cache.classify.current_editting_classify.info"),
			function(result) {
				alert("success", "优惠配置修改成功");
				current_editting_classify_vm.set(result.result);
				App.get("$Event.classify.cancel_config_classify")();
			});
	});
	//停用优惠配置
	App.set("$Event.classify.disable_buff_classify", function() {
		coAjax.put(classify_api_root.classify_disable_buff + current_editting_classify_vm.get("_id"),
			App.get("$Cache.classify.current_editting_classify"),
			function(result) {
				alert("success", "优惠配置停用成功");
				current_editting_classify_vm.set(result.result);
				App.get("$Event.classify.cancel_config_classify")();
			});
	});

	function _move_goods_list_to_classify(classify_id, goods_id_list, success_msg) {
		coAjax.put(classify_api_root.classify_move_goods_list_to_classify + classify_id, {
			goods_id_list: goods_id_list
		}, function(result) {
			alert("success", success_msg || "移动成功");
			//强制刷新
			_classify_id = _num = _page = null;
			hash_routie.emit("classify");
		});
	};
	//移除商品
	App.set("$Event.classify.remove_goods", function() {
		_move_goods_list_to_classify(undefined, jSouper.map(App.get("$Cache.classify.menu_box.pointer_goods_vms"), function(goods_vm) {
			return goods_vm.get("_id")
		}), "已经完成移除");
		App.get("$Event.classify.force_hidden_menu_box")();
	});

	//添加商品到分类文件夹
	App.set("$Event.classify.add_goods_to_classify", function(e, vm) {
		var classify_id = queryString.get("classify_id");
		if (!classify_id) {
			alert("warn", "请先选择分类文件夹并双击进入")
		}
		_move_goods_list_to_classify(classify_id, [vm.get("_id")]);
	});
	//打开商品所在的文件夹
	App.set("$Event.classify.open_goods_classify", function(e, vm) {
		hash_routie.hash({
			classify_id: vm.get("classify_id")
		});
	});
});